#include <ESP8266WiFi.h>
#include "http_server.h"
#include "effects.h"

#include <Ticker.h>  //导入定时器库
Ticker ticker;       //实例化定时器对象

int filter_count = 0;
int filter_sum = 0;

//#define FILTER_N 3

void sample() {     
  int sig = analogRead(A0);
  //Serial.printf("[%ld]timer read %d\r\n",millis(),sig);
  //Serial.printf("sig %d\r\n",sig);

#ifdef FILTER_N
  filter_sum += sig;
  filter_count++;
  
  if(filter_count == FILTER_N){
    sig = filter_sum / FILTER_N;
    filter_count = 0;
    filter_sum = 0;
    Serial.printf("filter %d\r\n",sig);
    set_sig(sig);
  }
#else
  set_sig(sig);
#endif
  

}

void setup() {
  //Serial.begin(115200);
  Serial.begin(250000);
//LED init
  led_init();

// WIFI init  
  wifi_init();
  
//WEB init
  webserver_init();

  ticker.attach_ms(5, sample);
}
int last_effects = 0;
#define MAX_EFFECTS_NAME  64

enum effects_index{
  MUSIC_0_0_DROP,
  MUSIC_1_1_DROP,
  MUSIC_2_1_DROP_FAST,
  MUSIC_3_1_DROP_SLOW,
  MUSIC_4_POP,
  MUSIC_5_DOT_DANCE,
  MUSIC_6_DANCE,
  MUSIC_7_DANCE_SINGLE_COLOR,
  MUSIC_8_EXTEND_FADE,  
  MUSIC_9_GATHER_FADE,
  MUSIC_10_FADE,
  MUSIC_11_FADE_SINGLE_COLOR,
  MUSIC_12_2_DROP,
  MUSIC_13_UP_DOWN,
  MUSIC_14_SEG_DROP,
  MUSIC_15_EXTEND,
  MUSIC_16_GATHER,
  MUSIC_17_FOLLOW,
  MAX_ARRAY_LEN = 18
};

typedef struct effects_info{
  int id;                              //灯效ID
  char effects_name[MAX_EFFECTS_NAME]; //灯效名称
  void (*effects_func)(void);          //灯效函数
}effects_info_t;

effects_info_t effects_array[] = {
  {MUSIC_0_0_DROP,          "music 0 drop", music_0_drop},  
  {MUSIC_1_1_DROP,          "music 1 drop", music_1_drop},  
  {MUSIC_2_1_DROP_FAST,     "music 1 drop", music_1_drop_fast},
  {MUSIC_3_1_DROP_SLOW,     "music 1 drop", music_1_drop_slow}, 
  {MUSIC_4_POP,             "music pop",    music_pop},
  {MUSIC_5_DOT_DANCE,       "music dot dance", music_dot_dance},
  {MUSIC_6_DANCE,           "music dance",  music_dance},
  {MUSIC_7_DANCE_SINGLE_COLOR, "music_dance_single_color", music_dance_single_color}, 
  {MUSIC_8_EXTEND_FADE,          "music_extend_fade", music_extend_fade},   
  {MUSIC_9_GATHER_FADE,          "music_gathered_fade", music_gathered_fade},
  {MUSIC_10_FADE,           "music_fade",   music_fade},
  {MUSIC_11_FADE_SINGLE_COLOR, "music_fade_single_color", music_fade_single_color},
  {MUSIC_12_2_DROP,         "music_2_drop ", music_2_drop},
  {MUSIC_13_UP_DOWN,        "music_up_down todo",  music_up_down},
  {MUSIC_14_SEG_DROP,       "music_seg_drop to check", music_seg_drop},
  {MUSIC_15_EXTEND,         "music_extend", music_extend},   
  {MUSIC_16_GATHER,          "music_gathered", music_gathered},
  {MUSIC_17_FOLLOW,          "music_follow", music_follow},
  {MAX_ARRAY_LEN, "", NULL},
};


void loop()
{
  //dance1();
  //musicWrite2();
  //musicWrite3();
  //musicWrite4();
  //music_drop();
  //music_pop();
  //music_double_drop();
  //music_extend();
  int index = get_effects();
  if(index != last_effects){
    clear_led();  
  }
  int i = 0; 
  if(index < MAX_ARRAY_LEN && effects_array[index].effects_func != NULL)
    effects_array[index].effects_func();
  else
    Serial.printf("wrong index %d\r\n", index);

#if 0
  switch(effects){
    case 0:
        music_0_drop();
        break;
    case 1:
        music_1_drop();
        break;
    case 2:
        music_1_drop_slow();
        break;
    case 3:
        music_1_drop_fast();
        break;
    case 4:
        music_pop();
        break;
    case 5:
        music_dot_dance();
        break;
    case 6:
        music_dance();
        break;
    case 7:
        music_dance_single_color();
        break;
    case 8:
        music_extend();
        break;
    case 9:
        music_gathered_debug();
        break;        
    case 10:
        music_fade();
        break;
    case 11:
        music_fade_single_color();
        break;  
    case 12:
       music_double_drop();      
       break;
    case 13:
      music_up_down();  
      break;
      
    default:
      break;
  }
#endif

  last_effects = index;
  
  //监听网页请求并处理
  webserver_handle();
}
